#include "comm/my_pool.h"
#include "comm/my_mem.h"
#include "comm/my_log.h"

void test_pool_mem_alloc(my_pool_t *pool)
{
    void *p;
    p = my_pool_mem_alloc(pool, 1);
    my_pool_mem_free(pool, p);
    p = my_pool_mem_alloc(pool, 100);
    my_pool_mem_free(pool, p);
    p = my_pool_mem_alloc(pool, 900);
    my_pool_mem_free(pool, p);
    p = my_pool_mem_alloc(pool, 900);
    my_pool_mem_free(pool, p);
    p = my_pool_mem_alloc(pool, 900);
    my_pool_mem_free(pool, p);
    p = my_pool_mem_alloc(pool, 900);
    my_pool_mem_free(pool, p);
    p = my_pool_mem_alloc(pool, 900);
    my_pool_mem_free(pool, p);
    p = my_pool_mem_alloc(pool, 900);
    my_pool_mem_free(pool, p);
    p = my_pool_mem_alloc(pool, 900);
    my_pool_mem_free(pool, p);
    p = my_pool_mem_alloc(pool, 1024);
    my_pool_mem_free(pool, p);
    p = my_pool_mem_alloc(pool, 1024);
    my_pool_mem_free(pool, p);
    p = my_pool_mem_alloc(pool, 4096);
    my_pool_mem_free(pool, p);
    p = my_pool_mem_alloc(pool, 8192);
    my_pool_mem_free(pool, p);
}

static inline void
my_test_cleanup(my_pool_t *pool, void *data)
{
    (void)pool;
    MY_LOG_DEBUG("cleanup %d", (int)(intptr_t)data);
}

void
test_pool_cleanup(my_pool_t *pool) 
{
    my_pool_cleanup_node_t *node1 = my_pool_add_cleanup(pool, my_test_cleanup, (void *)1);
    my_pool_cleanup_node_t *node2 = my_pool_add_cleanup(pool, my_test_cleanup, (void *)2);
    my_pool_cleanup_node_t *node3 = my_pool_add_cleanup(pool, my_test_cleanup, (void *)3);
    my_pool_add_cleanup(pool, my_test_cleanup, (void *)4);
    my_pool_add_cleanup(pool, my_test_cleanup, (void *)5);
    my_pool_del_cleanup(pool, node1);
    my_pool_del_cleanup(pool, node2);
    my_pool_del_cleanup(pool, node3);
    my_pool_add_cleanup(pool, my_test_cleanup, (void *)6);
    my_pool_add_cleanup(pool, my_test_cleanup, (void *)7);
    my_pool_add_cleanup(pool, my_test_cleanup, (void *)8);
}

int main() {
    my_pool_t *pool = my_pool_create();

    test_pool_mem_alloc(pool);

    test_pool_cleanup(pool);

    my_pool_destroy(pool);

    my_mem_assert_leak();

    MY_LOG_DEBUG("test pass");
    return 0;
}